/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fri.daolayer;

import fri.businessLayer.HistoryBeanRemote;
import fri.businessLayer.PaymentCodeBeanRemote;
import fri.entiry.FrHistory;
import fri.entiry.FrPaymentcode;

import java.util.List;
import java.util.UUID;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 *
 * @author NguyenDongUIT
 */
public class PaymentCodeDAO {
    
    public static String generateCode(String fromUserId, String toUserId, int currencyId, float value, String de, int status) {
        try {

            Context ic = new InitialContext();
            PaymentCodeBeanRemote rm = (PaymentCodeBeanRemote) ic.lookup("fri.businessLayer.PaymentCodeBeanRemote");
            
            
            //add history
            HistoryDAO.addHistory(fromUserId, de, status);
            //
            return rm.generateCode(fromUserId, toUserId, currencyId, value, de, status);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
            return null;
        }
        
    }
    
    public static List<FrPaymentcode> getPaymentCodeByUserId(String userId) {
        try {

            Context ic = new InitialContext();
            PaymentCodeBeanRemote rm = (PaymentCodeBeanRemote) ic.lookup("fri.businessLayer.PaymentCodeBeanRemote");
            return rm.getPaymentCodeByUserId(userId);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
            return null;
        }
        
    }
    
    public static FrPaymentcode getPaymentcode(String codeId) {
        try {

            Context ic = new InitialContext();
            PaymentCodeBeanRemote rm = (PaymentCodeBeanRemote) ic.lookup("fri.businessLayer.PaymentCodeBeanRemote");
            return rm.getPaymentcode(codeId);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
            return null;
        }
        
    }
    
    public static int setStatusOfPayment(String codeId, int state) {
        try {
            Context ic = new InitialContext();
            PaymentCodeBeanRemote rm = (PaymentCodeBeanRemote) ic.lookup("fri.businessLayer.PaymentCodeBeanRemote");
            return rm.setStatusOfPayment(codeId, state);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
            return 0;
        }
        
    }
    public static List<FrPaymentcode> getAllPaymentCode() {
        try {
            Context ic = new InitialContext();
            PaymentCodeBeanRemote rm = (PaymentCodeBeanRemote) ic.lookup("fri.businessLayer.PaymentCodeBeanRemote");
            return rm.getAllPaymentCode();

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
            return null;
        }
    }
    
    public static FrPaymentcode getPaymentCodeFromPaymentCode(String paymentCode) {
        try {
            Context ic = new InitialContext();
            PaymentCodeBeanRemote rm = (PaymentCodeBeanRemote) ic.lookup("fri.businessLayer.PaymentCodeBeanRemote");
            return rm.getPaymentCodeFromPaymentCode(paymentCode);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
            return null;
        }
    }
    public static int applyPaymentCode(String toUserId, String paymentCode)
    {
        try {
            Context ic = new InitialContext();
            PaymentCodeBeanRemote rm = (PaymentCodeBeanRemote) ic.lookup("fri.businessLayer.PaymentCodeBeanRemote");
            
            //add history
            //HistoryDAO.addHistory(toUserId, "Da thanh toan bang ma nay", 0);
            //
            return rm.applyPaymentCode(toUserId, paymentCode);
            

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
            return 0;
        }
        
    }
}
